<template>
  <view class="order-container">
    <view class="card-list">
      <uni-card
        v-for="(item, index) in deviceInfomate"
        :key="index"
        class="card-item"
        :class="['select', isSelected(item.id) ? 'select-border' : '']"
        @click="selectCard(item.id)"
      >
        <text class="device">{{ item.robotName }}</text>
        <text class="price">
          {{ isSelected(item.id) ? '已选中' : item.robotPrice + '元' }}
        </text>
      </uni-card>
    </view>
    <view class="device-num">
      <view class="num">
        <text>设备数量</text>
        <span>{{ devieceNumber }}</span>
      </view>
      <view class="icon">
        <uni-icons @click="increaseDeviceNumber()" type="plusempty" size="20"></uni-icons>
      </view>
    </view>
    <button type="primary" @click="submit()">立即充值<text>{{ TotelPrice }}</text></button>
	<view class="success-msg" v-if="showSuccessMsg">充值成功</view>
  </view>
</template>

<script>
import { robotList, addOrder, userID } from '../../../api/system/user.js'

export default {
  data() {
    return {
      devieceNumber: 0,
      deviceInfomate: [],
      selectedCard: null,
      selectedCardInfo: [],
	  showSuccessMsg: false  // 新增属性
    }
  },
  onLoad() {
    this.deviceInformation()
  },
  mounted() {
    this.deviceInformation()
  },
  methods: {
    async myUser() {
      const userId = await userID()
      console.log('获取信息成功', userId)
      return userId
    },
    deviceInformation() {
      robotList()
        .then(res => {
          console.log('设备信息请求成功', res)
          this.deviceInfomate = res
        })
        .catch(error => {
          console.error('设备信息请求失败', error)
        })
    },
    increaseDeviceNumber() {
      this.devieceNumber++
    },
    selectCard(cardId) {
      if (this.selectedCard === cardId) {
        // 如果设备已经被选中，则取消选中
        this.selectedCard = null
        this.selectedCardInfo = []
        this.devieceNumber = 0 // 重置数量为 0
      } else {
        // 否则，选中设备
        this.selectedCard = cardId
        this.devieceNumber = 0 // 重置数量为 0
        const selectedCard = this.deviceInfomate.find(item => item.id === cardId)
        if (selectedCard) {
          this.selectedCardInfo = [selectedCard]
        }
      }
    },
    isSelected(cardId) {
      return this.selectedCard === cardId
    },
    async submit() {
      const userId = await this.myUser()
      for (const selectedCard of this.selectedCardInfo) {
        const { robotName, robotPrice } = selectedCard
        const timeStamp = new Date().getTime()
        const data = new Date(timeStamp)
        const year = data.getFullYear()
        const month = data.getMonth() + 1
        const date = data.getDate()
        const hours = data.getHours()
        const minute = data.getMinutes()
        const formattedDate = `${year}-${month}-${date} ${hours}:${minute}`
        const totalPrice = robotPrice * this.devieceNumber
        const params = {
          orderName: robotName,
          orderNumber: this.devieceNumber,
          orderPrice: totalPrice,
          orderState: '0',
          orderTime: formattedDate,
          userId: userId
        }
        addOrder(params.orderName, params.orderNumber, params.orderPrice, params.orderState, params.orderTime, params.userId)
          .then(res => {
            console.log('请求成功', res)
			this.showSuccessMsg = true; // 显示成功提示
			      setTimeout(() => {
			        this.showSuccessMsg = false; // 一段时间后隐藏成功提示
			      }, 3000); // 3秒钟后隐藏
          })
      }
    }
  },
  computed: {
    TotelPrice() {
      let totalPrice = 0
      const selectedCard = this.deviceInfomate.find(item => item.id === this.selectedCard)
      if (selectedCard) {
        totalPrice = selectedCard.robotPrice * this.devieceNumber
      }
      return totalPrice
    }
  }
}
</script>

<style lang="scss">
.order-container {
  height: calc(100vh - 200px);
  padding: 10px 0;

  .card-list {
    width: 100%;
    display: flex;
    background-color: #fff;
    padding: 0;

    .card-item {
      width: 80px;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 50px;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.03);

      .price {
        font-size: 1px;
        color: #999;
        display: block;
      }

      .device {
        font-size: 8px;
        font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans';
        .number {
          font-size: 13px;
          font-weight: bold;
          color: #000;
          font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande';
        }
      }
    }
  }

  .device-num {
    display: flex;
    justify-content: space-between;
    border: 1px solid #ccc;
    margin: 10px;
    padding: 10px;

    .icon {
      #minus {
        font-size: 20px;
      }
    }
  }

  button {
    padding: 10px auto;
  }
}

.uni-card__content {
  line-height: 0;
  padding-top: 10px;
  background-color: #fff;
}

.select {
  background-color: rgba(0, 0, 3, 0.2);
}

.select-border {
  border: 2px solid blue;
}
.success-msg {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  padding: 10px;
  background-color: #ccc;
  color: green;
  border-radius: 5px;
}
</style>